package com.itheima.homework.homework5;

public class Demo5 {
    public static void main(String[] args) {
        //创建box对象
        Box box = new Box();
        int day = 10;
        //启动送货线程
        new Thread(() -> {
            for (int i = 1; i <= 10; i++) {
                synchronized (box) {
                    if (box.isFlag()) {//如果为真说明有奶
                        try {
                            box.wait();
                            i--;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        box.put(i);
                        //修改状态
                        box.setFlag(true);
                        //叫醒线程
                        box.notifyAll();
                    }
                }
            }


        }).start();
        //启动取货线程
        new Thread(() -> {
            int count = 0;
            while (true) {
                synchronized (box) {
                    if (count == day) {
                        break;
                    }
                    if(box.isFlag()){
                        box.take();
                        //修改
                        box.setFlag(false);
                        //叫醒
                        box.notifyAll();
                    }else {
                        try {
                            box.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();

    }
}

